//
//  GridViewItem.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-9.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "GridViewItem.h"
#import "FileManager.h"
#import "Singleton.h"
@interface GridViewItem(Private)
-(BOOL) checkPointInRange:(CGPoint) point Range:(CGRect) rect;
@end

@implementation GridViewItem
@synthesize delegate;
@synthesize reuseIdentifier;

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];//UIView默认背景颜色
        imageBkg = [[UIImageView alloc] init];
        
        imageView = [[UIImageView alloc] init];
        [imageView setTag:TagOfUIImageView];
        
        imageRecommend = [UIImageView new];
        imageRecommend.tag = TagOfUIImageView + 1;
        imageRecommend.image = [FileManager getImage:@"plurecommend_bkg.png" type:3 withRect:imageRecommend.frame];
        
        labelName = [[UILabel alloc] init];
        [labelName setTag:TagOfUILabel];
//        [labelName setAdjustsFontSizeToFitWidth:YES];//自动调整字体大小
        [labelName setFont:[UIFont fontWithName:@"Arial" size:24.0f]];//默认字体大小
        [labelName setTextColor:[UIColor whiteColor]];//默认字体白色
        [labelName setTextAlignment:UITextAlignmentCenter];//默认字体居中对齐
        [labelName setBackgroundColor:[UIColor clearColor]];//默认没有背景颜色
        
        [self addSubview:imageBkg];
        [self addSubview:imageView];
        [self addSubview:imageRecommend];
        [self addSubview:labelName];
        [self add1fingerTap];
    }
    return self;
}
- (void) setData:(id)data{}
- (void) setSelectedItem:(BOOL)selected{}
- (void) add1fingerTap{//手指单击
	UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    recognizer.numberOfTouchesRequired = 1;//多少根手指
    recognizer.numberOfTapsRequired = 1;//点击几次
    recognizer.delegate = self;
	[self addGestureRecognizer:recognizer];
	[recognizer release];	
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
	if ([touch.view isKindOfClass:[UIButton class]]){
		MyNSLog(@"按在Button上了");
		return NO;
	}
    //这里如何屏蔽UILabel？？？
	return YES;
}

//这个方法最好不要被重载
- (void) handleTapFrom:(UITapGestureRecognizer *)recognizer {//点击单个item触发事件
    CGPoint location = [recognizer locationInView:self];
    if ([self checkPointInRange:location Range:[self viewWithTag:TagOfUIImageView].frame]) {
        if (self.delegate) {            
            if (AppSession.isAnimating) {
                MyNSLog(@"GridViewItem上一个动画还未结束!!!");
                return;
            }else {
                if (AppSession.isPopOutFullScreen) {
                    MyNSLog(@"GridViewItem大图窗口已经弹出!!!");
                }else {
                    AppSession.isAnimating = AppSession.isPopOutFullScreen = YES;
                    [AppSession playMusic:VoiceFile(@"clicked.caf")];
                    [self.delegate gridViewItemTapped:self];
                }
            }
        }else{
            MyNSLog(@"没有实现UIGridViewItemDelegate");
        }
    }
}

-(BOOL) checkPointInRange:(CGPoint) point Range:(CGRect) rect{
    if (point.x<rect.origin.x||point.x>rect.origin.x+rect.size.width||
        point.y<rect.origin.y||point.y>rect.origin.y+rect.size.height) {
        return NO;
    }else{
        return YES;
    }
}
-(void) dealloc{
    MyNSLog(@"GridViewItem[%d] is deallocing...", self.tag-TagOfUIGridViewItem);
    [imageBkg release];
    [imageView release];
    [imageRecommend release];
    [labelName release];
    [reuseIdentifier release];
    [super dealloc];
}
@end
